﻿/**
 * Created by Administrator on 2017/11/28.
 */
function prompt(title) {
    /*str = '<div class="tishi" style="box-sizing:border-box;font-size: 30px;z-index: 1111;text-align: ' +
        'center;width: 500px;height: 120px;line-height:120px;position: absolute;background:#ffe488;border-radius: 20px;top: 280px;left: 390px;color: black;">' + title +
        '</div>';
    $("body").append(str);
    setTimeout(function () {
        $('.tishi').hide()
    }, 2000);*/

    var mydiv = document.createElement('div');
    mydiv.className = 'tishi';
    mydiv.style.boxSizing = 'border-box';
    mydiv.style.fontSize = '30px';
    mydiv.style.width = '800px';
    mydiv.style.height = '120px';
    mydiv.style.position = 'absolute';
    mydiv.style.background = '#ffe488';
    mydiv.style.zIndex = 1111;
    mydiv.style.top = '280px';
    mydiv.style.left = '300px';
    mydiv.style.lineHeight = '120px';
    mydiv.style.textAlign = 'center';
    mydiv.style.color = 'black';
    mydiv.style.borderRadius = '20px';
    mydiv.innerText = title;
    //mydiv.innerText = title;

    //$("body").append(str);
    document.body.appendChild(mydiv);

    setTimeout(function () {
        //document.getElementsByClassName('tishi')[0].style.display='none';
        document.body.removeChild(mydiv);
    }, 2000);
};
var FOCUS = 'ensure';
var tv_no = getParam('tv_no');
var tv_card = getParam('tv_card');
var record_id = getParam('record_id');
var prize_name = getParam('prize_name');
// var base_id=getParam("base_id");
var backurl = getParam("backurl");
var urladdress = getParam("urladdress");
var mobile = "";
var type = 0;
var isInput = true;
// $('#name').html(prize_name);
document.getElementById('name').innerText = prize_name;
// $('#phone').focus();
document.getElementById('phone').focus();
getCustomer();
var iAlert = new iAlert();
document.onirkeypress = keyDown;
document.onkeydown = keyDown;

function keyDown(evt) {
    evt = (evt) ? evt : ((window.event) ? window.event : "") //兼容IE和Firefox获得keyBoardEvent对象
    var keyCode = evt.keyCode ? evt.keyCode : evt.which;
    // prompt(keyCode);
    switch (keyCode) {

        // case KEY.RIGHT:
        // case KEY.RIGHT2:
        //     $('#edit').css('background', 'url(./images/lottery/xg_s.png)');
        //     $('#ensure').css('background', 'url(./images/lottery/qd.png)');
        //     FOCUS = "edit";
        //     break;
        // case KEY.LEFT:
        // case KEY.LEFT2:
        //     $('#edit').css('background', 'url(./images/lottery/xg.png)');
        //     $('#ensure').css('background', 'url(./images/lottery/qd_s.png)');
        //     FOCUS = "ensure";
        //     break;
        case KEY.UP:
        case KEY.UP2:
            break;
        case KEY.DOWN:
        case KEY.DOWN2:
            break;
        case KEY.ENTER:
            switch (FOCUS) {
                case "ensure":
                    upLoadPhone();
                    break;
                case "save":
                    // 约定：如果url是“portal”，则跳到固定页。如果url为空，则返回上一页。如果url不是“portal”，则跳到url的页面
                    if ((urladdress.toLowerCase()) == "portal") {
                        if (typeof (Android) != "undefined") { // 安卓盒子
                            iPanel.eventFrame.openPortal();
                        } else {
                            window.location.href = "http://10.65.255.6:8080/portalrouting/pr.do?stbid=" + hardware.STB.serialNumber;
                        }
                    } else if (urladdress == "" || urladdress == null) {
                        history.go(-1);
                    } else {
                        window.location.href = urladdress;
                    }
                    // window.location.href = 'index.html?tag=1';
                    break;
                case "error":
                    if (type == 0) {
                        doOk();
                    } else {
                        iAlert.close();
                    }
                    FOCUS = "ensure";
                    break;
                case "edit":
                    //$('#phone').html("");
                    document.getElementById('phone').innerHTML = '';
                    mobile = "";
                    isInput = true;
                    $('#edit').css('background', 'url(images/lottery/xg.png)');
                    $('#ensure').css('background', 'url(images/lottery/qd_s.png)');
                    FOCUS = "ensure";
                    break;

            }
            break;
        case KEY.NUMBER0:
            if (isInput) {
                mobile += "0";
                //$('#phone').html(mobile);
                document.getElementById('phone').innerHTML = mobile;
            }
            break;
        case KEY.NUMBER1:
            if (isInput) {
                mobile += "1";
                //$('#phone').html(mobile);
                document.getElementById('phone').innerHTML = mobile;
            }
            break;
        case KEY.NUMBER2:
            if (isInput) {
                mobile += "2";
                //$('#phone').html(mobile);
                document.getElementById('phone').innerHTML = mobile;
            }
            break;
        case KEY.NUMBER3:
            if (isInput) {
                mobile += "3";
                //$('#phone').html(mobile);
                document.getElementById('phone').innerHTML = mobile;
            }
            break;
        case KEY.NUMBER4:
            if (isInput) {
                mobile += "4";
                //$('#phone').html(mobile);
                document.getElementById('phone').innerHTML = mobile;
            }
            break;
        case KEY.NUMBER5:
            if (isInput) {
                mobile += "5";
                //$('#phone').html(mobile);
                document.getElementById('phone').innerHTML = mobile;
            }
            break;
        case KEY.NUMBER6:
            if (isInput) {
                mobile += "6";
                //$('#phone').html(mobile);
                document.getElementById('phone').innerHTML = mobile;
            }
            break;
        case KEY.NUMBER7:
            if (isInput) {
                mobile += "7";
                //$('#phone').html(mobile);
                document.getElementById('phone').innerHTML = mobile;
            }
            break;
        case KEY.NUMBER8:
            if (isInput) {
                mobile += "8";
                //$('#phone').html(mobile);
                document.getElementById('phone').innerHTML = mobile;
            }
            break;
        case KEY.NUMBER9:
            if (isInput) {
                mobile += "9";
                //$('#phone').html(mobile);
                document.getElementById('phone').innerHTML = mobile;
            }
            break;
        case KEY.BACK:
            //  prompt(111111)   
            if (isInput) {
                if (mobile.length > 0) {
                    mobile = mobile.substr(0, mobile.length - 1);
                    //$('#phone').html(mobile);
                    document.getElementById('phone').innerHTML = mobile;
                }
            }
            return 0;
            break;
        case KEY.EXIT:

        case KEY.MENU:
        case KEY.INTERACTIVE:
            // window.location.href='./index.html'
            setTimeout(function () {
                // 约定：如果url是“portal”，则跳到固定页。如果url为空，则返回上一页。如果url不是“portal”，则跳到url的页面
                if ((urladdress.toLowerCase()) == "portal") {
                    if (typeof (Android) != "undefined") { // 安卓盒子
                        iPanel.eventFrame.openPortal();
                    } else {
                        window.location.href = "http://10.65.255.6:8080/portalrouting/pr.do?stbid=" + hardware.STB.serialNumber;
                    }
                } else if (urladdress == "" || urladdress == null) {
                    history.go(-1);
                } else {
                    window.location.href = urladdress;
                }
                // window.location.href = './index.html'
            }, 1000)
            // window.location.go(-1)
            // window.history.go(-1)
            return 0;
            break;

    }
}

function upLoadPhone() {
    function trim(str) {
        return str.replace(/(^\s*)|(\s*$)/g, "")
    }
    var inputphone = document.getElementById('phone').innerText;
    //var phone = trim($("#phone").text());
    var phone = trim(inputphone);
    var phoneNumReg = /^1[3456789]\d{9}$/;
    if (phoneNumReg.test(phone)) {
        var ajax;
        if (window.XMLHttpRequest) {
            ajax = new XMLHttpRequest();
        } else {
            ajax = new ActiveXObject('Microsoft.XMLHTTP');
        }
        ajax.onreadystatechange = function () {
            if (ajax.readyState == 4 && ajax.status == 200) {
                var text = ajax.responseText;
                if (text.length != 0) {
                    var response = eval("(" + text + ")");
                }
                if (response.sts == 1) {
                    prompt("提交成功");
                    setTimeout(function () {

                        // 约定：如果url是“portal”，则跳到固定页。如果url为空，则返回上一页。如果url不是“portal”，则跳到url的页面
                        if ((urladdress.toLowerCase()) == "portal") {
                            if (typeof (Android) != "undefined") { // 安卓盒子
                                iPanel.eventFrame.openPortal();
                            } else {
                                window.location.href = "http://10.65.255.6:8080/portalrouting/pr.do?stbid=" + hardware.STB.serialNumber;
                            }
                        } else if (urladdress == "" || urladdress == null) {
                            history.go(-1);
                        } else {
                            window.location.href = urladdress;
                        }
                        // if(!backurl){
                        //     // history.go(-1)
                        //     window.location.href="./index.html"
                        //     // window.location.href="./record.html"
                        // }else{
                        //     window.location.href=decodeURIComponent(backurl)
                        // }
                    }, 2000)
                } else {
                    prompt(response.msg)
                }
            }
        }
        ajax.open('post', STATICURL + "Record/item", true);
        ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        ajax.send("record_id=" + record_id + "&mobile=" + phone + '&tv_card=' + tv_card + '&base_id=' + getCookie('base_id'));

    } else {
        // if (typeof (Android) != "undefined") {
        //     iAlert.hint = "<div style='font-size:25px;margin-top:50px;'>手机号输入有误222<div>";
        //     iAlert.open();
        //     type = 1;
        // } else {
        prompt('手机号输入有误');
        // type = 0;
        // }
        // FOCUS = "error";
        mobile = "";
        //$('#phone').html("");
        document.getElementById('phone').innerHTML = '';
    }
}

function getCustomer() {
    var ajax;
    if (window.XMLHttpRequest) {
        ajax = new XMLHttpRequest();
    } else {
        ajax = new ActiveXObject('Microsoft.XMLHTTP');
    }
    ajax.onreadystatechange = function () {
        if (ajax.readyState == 4 && ajax.status == 200) {
            var text = ajax.responseText;
            if (text.length != 0) {
                var response = eval("(" + text + ")");
                if (null != response.user_name) {
                    $('#info').html("姓名：<span style='color:black'>" + response.user_name + "</span>");
                } else {
                    $('#info').html("姓名：");
                }
                if (null != response.user_phone) {
                    // $('#phone').html(response.user_phone);
                } else {
                    isInput = true;
                }
            }
            ajax = null;
        }
    }
    ajax.open('post', STATICURL + "Lottery/getCustomer", true);
    ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    ajax.send('&tv_card=' + tv_card + '&base_id=' + getCookie('base_id'));

}